<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android Settings(设置)语言的切换和添加原理流程简析 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android Settings(设置)语言的切换和添加原理流程简析">
<meta property="og:type" content="article">
<meta property="og:title" content="Android Settings(设置)语言的切换和添加原理流程简析">
<meta property="og:url" content="http://longzhiye.top/2023/04/22/2023-04-22/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android Settings(设置)语言的切换和添加原理流程简析">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-21T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.904Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">

  <meta name="keywords" content=",Android">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2023/04/22/2023-04-22/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2023/04/22/2023-04-22/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-8.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android Settings(设置)语言的切换和添加原理流程简析</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2023-04-22 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2023/04/22/2023-04-22/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android Settings(设置)语言的切换和添加原理流程简析&url=http://longzhiye.top/2023/04/22/2023-04-22/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android Settings(设置)语言的切换和添加原理流程简析&url=http://longzhiye.top/2023/04/22/2023-04-22/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2023/04/22/2023-04-22/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2023/04/22/2023-04-22/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2023/04/22/2023-04-22/&title=Android Settings(设置)语言的切换和添加原理流程简析" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android Settings(设置)语言的切换和添加原理流程简析&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2023/04/22/2023-04-22/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2023/04/22/2023-04-22/&text=Android Settings(设置)语言的切换和添加原理流程简析" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h2 id="Settings-部分"><a href="#Settings-部分" class="headerlink" title="Settings 部分"></a>Settings 部分</h2><h3 id="切换流程"><a href="#切换流程" class="headerlink" title="切换流程"></a>切换流程</h3><p>首先是语言和输入的设置界面<br><strong>src/com/android/settings/language/LanguageAndInputSettings.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line"><span class="comment">// 注释1_1：加载了language_and_input 这个布局文件</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="type">int</span> <span class="title function_">getPreferenceScreenResId</span><span class="params">()</span> &#123;</span><br><span class="line">   <span class="keyword">return</span> R.xml.language_and_input;</span><br><span class="line">&#125;</span><br><span class="line">......</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>然后看下<strong>language_and_input.xml</strong>这个布局文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">PreferenceScreen</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:settings</span>=<span class="string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:title</span>=<span class="string">&quot;@string/language_settings&quot;</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 注释1_2: 这里的 phone_language 即对应着 界面上的语言选项 </span></span><br><span class="line"><span class="comment">	             下面的 keyboards_category 则对应着键盘的设置选项</span></span><br><span class="line"><span class="comment">	             注意：这里的 fragment 属性设置的是com.android.settings.localepicker.LocaleListEditor</span></span><br><span class="line"><span class="comment">	             即我们点开语言选项，即由这个页面来实现--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">Preference</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:key</span>=<span class="string">&quot;phone_language&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:title</span>=<span class="string">&quot;@string/phone_language&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:icon</span>=<span class="string">&quot;@drawable/ic_translate_24dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fragment</span>=<span class="string">&quot;com.android.settings.localepicker.LocaleListEditor&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">PreferenceCategory</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:key</span>=<span class="string">&quot;keyboards_category&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:title</span>=<span class="string">&quot;@string/keyboard_and_input_methods_category&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">Preference</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:key</span>=<span class="string">&quot;virtual_keyboard_pref&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:title</span>=<span class="string">&quot;@string/virtual_keyboard_category&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:fragment</span>=<span class="string">&quot;com.android.settings.inputmethod.VirtualKeyboardFragment&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">settings:keywords</span>=<span class="string">&quot;@string/keywords_virtual_keyboard&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">Preference</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:key</span>=<span class="string">&quot;physical_keyboard_pref&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:title</span>=<span class="string">&quot;@string/physical_keyboard_title&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:summary</span>=<span class="string">&quot;@string/summary_placeholder&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:fragment</span>=<span class="string">&quot;com.android.settings.inputmethod.PhysicalKeyboardFragment&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">PreferenceCategory</span>&gt;</span></span><br><span class="line">    ......</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>另外，Settings里的界面基本都是Preference(界面xml) 和 xxxController(数据逻辑管理) ,语言的controller 是 <strong>PhoneLanguagePreferenceController</strong> ，具体这里不再详细展开。</p>
<p>多语言的切换和添加页面：<br><strong>src/com/android/settings/localepicker/LocaleListEditor.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onCreate</span><span class="params">(Bundle savedInstanceState)</span> &#123;</span><br><span class="line">      <span class="built_in">super</span>.onCreate(savedInstanceState);</span><br><span class="line">      setHasOptionsMenu(<span class="literal">true</span>);</span><br><span class="line">   <span class="comment">// 注释1_4: 这里是已经添加列表数据的初始化，获取和填充 这里的 LocaleStore getUserLocaleList（）</span></span><br><span class="line">   <span class="comment">// 所涉及的 LocaleList LocalePiker 都是frameworks层的实现，后面会说到</span></span><br><span class="line">      LocaleStore.fillCache(<span class="built_in">this</span>.getContext());</span><br><span class="line">      <span class="keyword">final</span> List&lt;LocaleStore.LocaleInfo&gt; feedsList = getUserLocaleList();</span><br><span class="line">      mAdapter = <span class="keyword">new</span> <span class="title class_">LocaleDragAndDropAdapter</span>(<span class="built_in">this</span>.getContext(), feedsList);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">public</span> View <span class="title function_">onCreateView</span><span class="params">(LayoutInflater inflater, ViewGroup container, Bundle savedInstState)</span> &#123;</span><br><span class="line">      <span class="keyword">final</span> <span class="type">View</span> <span class="variable">result</span> <span class="operator">=</span> <span class="built_in">super</span>.onCreateView(inflater, container, savedInstState);</span><br><span class="line">      <span class="comment">//注释1_4: 这里加载了布局 locale_order_list.xml 比较简单 一个列表 和 一个添加按钮</span></span><br><span class="line">      <span class="keyword">final</span> <span class="type">View</span> <span class="variable">myLayout</span> <span class="operator">=</span> inflater.inflate(R.layout.locale_order_list, (ViewGroup) result);</span><br><span class="line"></span><br><span class="line">      configureDragAndDrop(myLayout);</span><br><span class="line">      <span class="keyword">return</span> result;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">......</span><br><span class="line">   <span class="comment">// 注释1_5: 这里的方法，对应上面注释1_4。即已添加的语言列表数据</span></span><br><span class="line">   <span class="keyword">private</span> List&lt;LocaleStore.LocaleInfo&gt; getUserLocaleList() &#123;</span><br><span class="line">      <span class="keyword">final</span> List&lt;LocaleStore.LocaleInfo&gt; result = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">      <span class="keyword">final</span> <span class="type">LocaleList</span> <span class="variable">localeList</span> <span class="operator">=</span> LocalePicker.getLocales();</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; localeList.size(); i++) &#123;</span><br><span class="line">         <span class="type">Locale</span> <span class="variable">locale</span> <span class="operator">=</span> localeList.get(i);</span><br><span class="line">         result.add(LocaleStore.getLocaleInfo(locale));</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> result;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">configureDragAndDrop</span><span class="params">(View view)</span> &#123;</span><br><span class="line">      <span class="keyword">final</span> <span class="type">RecyclerView</span> <span class="variable">list</span> <span class="operator">=</span> view.findViewById(R.id.dragList);</span><br><span class="line">      <span class="keyword">final</span> <span class="type">LocaleLinearLayoutManager</span> <span class="variable">llm</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">LocaleLinearLayoutManager</span>(getContext(), mAdapter);</span><br><span class="line">      llm.setAutoMeasureEnabled(<span class="literal">true</span>);</span><br><span class="line">      list.setLayoutManager(llm);</span><br><span class="line"></span><br><span class="line">      list.setHasFixedSize(<span class="literal">true</span>);</span><br><span class="line">      mAdapter.setRecyclerView(list);</span><br><span class="line">      list.setAdapter(mAdapter);</span><br><span class="line"></span><br><span class="line">      mAddLanguage = view.findViewById(R.id.add_language);</span><br><span class="line">      <span class="comment">// 注释1_6: 这里是添加按钮的监听</span></span><br><span class="line">      mAddLanguage.setOnClickListener(<span class="keyword">new</span> <span class="title class_">View</span>.OnClickListener() &#123;</span><br><span class="line">         <span class="meta">@Override</span></span><br><span class="line">         <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onClick</span><span class="params">(View v)</span> &#123;</span><br><span class="line">               FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider()</span><br><span class="line">                     .logSettingsTileClick(INDEX_KEY_ADD_LANGUAGE, getMetricsCategory());</span><br><span class="line"></span><br><span class="line">               <span class="keyword">final</span> <span class="type">Intent</span> <span class="variable">intent</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Intent</span>(getActivity(),</span><br><span class="line">                     LocalePickerWithRegionActivity.class);</span><br><span class="line">               startActivityForResult(intent, REQUEST_LOCALE_PICKER);</span><br><span class="line">         &#125;</span><br><span class="line">   &#125;);</span><br><span class="line">   </span><br><span class="line">   ......</span><br></pre></td></tr></table></figure>

<p>已添加语言的由前面分析可以知道，布局<strong>locale_order_list.xml</strong>里面是由</p>
<p>自定义RecyclerView列表：<strong>src/com/android/settings/localepicker/LocaleRecyclerView.java</strong></p>
<p>数据适配器：<strong>src/com/android/settings/localepicker/LocaleDragAndDropAdapter.java</strong></p>
<p>来组合实现的，先看下</p>
<p><strong>LocaleDragAndDropAdapter.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line">      <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doTheUpdate</span><span class="params">()</span> &#123;</span><br><span class="line">      <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> mFeedItemList.size();</span><br><span class="line">      <span class="keyword">final</span> Locale[] newList = <span class="keyword">new</span> <span class="title class_">Locale</span>[count];</span><br><span class="line"></span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; count; i++) &#123;</span><br><span class="line">         <span class="keyword">final</span> LocaleStore.<span class="type">LocaleInfo</span> <span class="variable">li</span> <span class="operator">=</span> mFeedItemList.get(i);</span><br><span class="line">         newList[i] = li.getLocale();</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">final</span> <span class="type">LocaleList</span> <span class="variable">ll</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">LocaleList</span>(newList);</span><br><span class="line">      <span class="comment">// 注释1_7: 前面都是做准备工作，这里调用这个做更实质的处理</span></span><br><span class="line">      updateLocalesWhenAnimationStops(ll);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="keyword">private</span> <span class="type">LocaleList</span> <span class="variable">mLocalesToSetNext</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">   <span class="keyword">private</span> <span class="type">LocaleList</span> <span class="variable">mLocalesSetLast</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">   </span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateLocalesWhenAnimationStops</span><span class="params">(<span class="keyword">final</span> LocaleList localeList)</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (localeList.equals(mLocalesToSetNext)) &#123;</span><br><span class="line">         <span class="keyword">return</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// This will only update the Settings application to make things feel more responsive,</span></span><br><span class="line">      <span class="comment">// the system will be updated later, when animation stopped.</span></span><br><span class="line">      LocaleList.setDefault(localeList);</span><br><span class="line"></span><br><span class="line">      mLocalesToSetNext = localeList;</span><br><span class="line">      <span class="keyword">final</span> RecyclerView.<span class="type">ItemAnimator</span> <span class="variable">itemAnimator</span> <span class="operator">=</span> mParentView.getItemAnimator();</span><br><span class="line">      itemAnimator.isRunning(<span class="keyword">new</span> <span class="title class_">RecyclerView</span>.ItemAnimator.ItemAnimatorFinishedListener() &#123;</span><br><span class="line">         <span class="meta">@Override</span></span><br><span class="line">         <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationsFinished</span><span class="params">()</span> &#123;</span><br><span class="line">               <span class="keyword">if</span> (mLocalesToSetNext == <span class="literal">null</span> || mLocalesToSetNext.equals(mLocalesSetLast)) &#123;</span><br><span class="line">                  <span class="comment">// All animations finished, but the locale list did not change</span></span><br><span class="line">                  <span class="keyword">return</span>;</span><br><span class="line">               &#125;</span><br><span class="line">            <span class="comment">// 注释1_8: 当已添加的语言数目发生了改变，则调用frameworks 层的 </span></span><br><span class="line">            <span class="comment">// LocalePicker的 updateLocales 方法处理，具体后面看</span></span><br><span class="line">               LocalePicker.updateLocales(mLocalesToSetNext);</span><br><span class="line">               mLocalesSetLast = mLocalesToSetNext;</span><br><span class="line">               <span class="keyword">new</span> <span class="title class_">ShortcutsUpdateTask</span>(mContext).execute();</span><br><span class="line"></span><br><span class="line">               mLocalesToSetNext = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">               mNumberFormatter = NumberFormat.getNumberInstance(Locale.getDefault());</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>初看可能有点疑惑，这个<strong>doTheUpdate</strong>方法 在哪里调用和触发的呢？</p>
<p><strong>LocaleRecyclerView.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">onTouchEvent</span><span class="params">(MotionEvent e)</span> &#123;</span><br><span class="line">   <span class="keyword">if</span> (e.getAction() == MotionEvent.ACTION_UP || e.getAction() == MotionEvent.ACTION_CANCEL) &#123;</span><br><span class="line">      <span class="type">LocaleDragAndDropAdapter</span> <span class="variable">adapter</span> <span class="operator">=</span> (LocaleDragAndDropAdapter) <span class="built_in">this</span>.getAdapter();</span><br><span class="line">      <span class="keyword">if</span> (adapter != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 注释1_9: 这里一目了然，当列表的触摸事件手指离开的时候，便会触发这个更新</span></span><br><span class="line">            adapter.doTheUpdate();</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">return</span> <span class="built_in">super</span>.onTouchEvent(e);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>关于Settings 切换已选语言的处理流程基本就这些了，然后说下添加的流程。</p>
<h3 id="添加流程"><a href="#添加流程" class="headerlink" title="添加流程"></a>添加流程</h3><p>前面的介绍在 注释1_6 处，<strong>LocaleListEditor.java</strong>的添加语言的按钮，点击监听事件里是流程的入口，会跳转</p>
<p><strong>src/com/android/settings/localepicker/LocalePickerWithRegionActivity.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onCreate</span><span class="params">(Bundle savedInstanceState)</span> &#123;</span><br><span class="line">   <span class="built_in">super</span>.onCreate(savedInstanceState);</span><br><span class="line">   getActionBar().setDisplayHomeAsUpEnabled(<span class="literal">true</span>);</span><br><span class="line"><span class="comment">// 注释1_10: 这里的页面以及逻辑实现都交给了 LocalePickerWithRegion ，</span></span><br><span class="line"><span class="comment">// 这个类的实现也是在frameworks 层</span></span><br><span class="line">   <span class="keyword">final</span> <span class="type">LocalePickerWithRegion</span> <span class="variable">selector</span> <span class="operator">=</span> LocalePickerWithRegion.createLanguagePicker(</span><br><span class="line">            <span class="built_in">this</span>, LocalePickerWithRegionActivity.<span class="built_in">this</span>, <span class="literal">false</span> <span class="comment">/* translate only */</span>);</span><br><span class="line">   getFragmentManager()</span><br><span class="line">            .beginTransaction()</span><br><span class="line">            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)</span><br><span class="line">            .replace(android.R.id.content, selector)</span><br><span class="line">            .addToBackStack(PARENT_FRAGMENT_NAME)</span><br><span class="line">            .commit();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>所以，添加的流程，Settings 的添加流程到这里也就结束了</p>
<h2 id="Frameworks-部分"><a href="#Frameworks-部分" class="headerlink" title="Frameworks 部分"></a>Frameworks 部分</h2><h3 id="切换流程-1"><a href="#切换流程-1" class="headerlink" title="切换流程"></a>切换流程</h3><p>由前面分析可知：切换语言Settings 处理跳转到 framework 的入口是<strong>LocalePicker</strong>的<strong>updateLocales</strong>方法</p>
<p><strong>base/core/java/com/android/internal/app/LocalePicker.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line"></span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * Requests the system to update the list of system locales.</span></span><br><span class="line"><span class="comment">   * Note that the system looks halted for a while during the Locale migration,</span></span><br><span class="line"><span class="comment">   * so the caller need to take care of it.</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">   <span class="meta">@UnsupportedAppUsage</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">updateLocales</span><span class="params">(LocaleList locales)</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (locales != <span class="literal">null</span>) &#123;</span><br><span class="line">         locales = removeExcludedLocales(locales);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// Note: the empty list case is covered by Configuration.setLocales().</span></span><br><span class="line"></span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">         <span class="keyword">final</span> <span class="type">IActivityManager</span> <span class="variable">am</span> <span class="operator">=</span> ActivityManager.getService();</span><br><span class="line">         <span class="keyword">final</span> <span class="type">Configuration</span> <span class="variable">config</span> <span class="operator">=</span> am.getConfiguration();</span><br><span class="line">      <span class="comment">// 注释2_1：这里对切换语言后的数据封装到Configuration里，用于后面流程处理</span></span><br><span class="line">         config.setLocales(locales);</span><br><span class="line">         config.userSetLocale = <span class="literal">true</span>;</span><br><span class="line">      <span class="comment">// 注释2_2： 这里通过ActivityManager的一个Binder服务，调用</span></span><br><span class="line">      <span class="comment">// updatePersistentConfigurationWithAttribution 继续处理	</span></span><br><span class="line">         am.updatePersistentConfigurationWithAttribution(config,</span><br><span class="line">                  ActivityThread.currentOpPackageName(), <span class="literal">null</span>);</span><br><span class="line">         <span class="comment">// Trigger the dirty bit for the Settings Provider.</span></span><br><span class="line">         BackupManager.dataChanged(<span class="string">&quot;com.android.providers.settings&quot;</span>);</span><br><span class="line">      &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">         <span class="comment">// Intentionally left blank</span></span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">......</span><br></pre></td></tr></table></figure>

<p>这里是通过 Binder 获取 ActivityManager 的一个服务代理对象，来处理 实现方法是 <strong>updatePersistentConfigurationWithAttribution</strong><br>这里ActivityManager的Binder 实际处理对象是：<br><strong>base/services/core/java/com/android/server/am/ActivityManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updatePersistentConfiguration</span><span class="params">(Configuration values)</span> &#123;</span><br><span class="line">   updatePersistentConfigurationWithAttribution(values,</span><br><span class="line">            Settings.getPackageNameForUid(mContext, Binder.getCallingUid()), <span class="literal">null</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updatePersistentConfigurationWithAttribution</span><span class="params">(Configuration values,</span></span><br><span class="line"><span class="params">      String callingPackage, String callingAttributionTag)</span> &#123;</span><br><span class="line">   enforceCallingPermission(CHANGE_CONFIGURATION, <span class="string">&quot;updatePersistentConfiguration()&quot;</span>);</span><br><span class="line">   enforceWriteSettingsPermission(<span class="string">&quot;updatePersistentConfiguration()&quot;</span>, callingPackage,</span><br><span class="line">            callingAttributionTag);</span><br><span class="line">   <span class="keyword">if</span> (values == <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">NullPointerException</span>(<span class="string">&quot;Configuration must not be null&quot;</span>);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="type">int</span> <span class="variable">userId</span> <span class="operator">=</span> UserHandle.getCallingUserId();</span><br><span class="line"><span class="comment">// 注释2_3：可以看到，这里流程将处理方法又传递给了 mActivityTaskManager 实例的updatePersistentConfiguration 方法</span></span><br><span class="line">   mActivityTaskManager.updatePersistentConfiguration(values, userId);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的mActivityTaskManager的实例即 <strong>ActivityTaskManagerService.java</strong>，来看看内部实现</p>
<p><strong>base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java</strong><br>通过内部层层调用，以及一些判断条件的筛选，最后会执行到(方法内部处理逻辑很多，挑重点看下)：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** Update default (global) configuration and notify listeners about changes. */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">updateGlobalConfigurationLocked</span><span class="params">(<span class="meta">@NonNull</span> Configuration values, <span class="type">boolean</span> initLocale,</span></span><br><span class="line"><span class="params">      <span class="type">boolean</span> persistent, <span class="type">int</span> userId)</span> &#123;</span><br><span class="line">      </span><br><span class="line">   ......</span><br><span class="line">   <span class="comment">// 注释2_4：这里对切换语言后封装的Configuration做后续处理的预检查和判断</span></span><br><span class="line">      <span class="keyword">if</span> (!initLocale &amp;&amp; !values.getLocales().isEmpty() &amp;&amp; values.userSetLocale) &#123;</span><br><span class="line">      <span class="keyword">final</span> <span class="type">LocaleList</span> <span class="variable">locales</span> <span class="operator">=</span> values.getLocales();</span><br><span class="line">      <span class="type">int</span> <span class="variable">bestLocaleIndex</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">if</span> (locales.size() &gt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (mSupportedSystemLocales == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 注释2_5：这里是获取系统资源配置支持的语言，也就是说实际能真的支持的语言</span></span><br><span class="line">               mSupportedSystemLocales = Resources.getSystem().getAssets().getLocales();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 注释2_6：这里是通过方法计算得到在所有支持语言列表里 最匹配的语言的所在列表的 索引</span></span><br><span class="line">            <span class="comment">// 具体算法可getFirstMatchIndex 一路点进去看</span></span><br><span class="line">            bestLocaleIndex = Math.max(<span class="number">0</span>, locales.getFirstMatchIndex(mSupportedSystemLocales));</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 注释2_7：这里是修改系统属性值，即 当前系统的默认语言</span></span><br><span class="line">      SystemProperties.set(<span class="string">&quot;persist.sys.locale&quot;</span>,</span><br><span class="line">               locales.get(bestLocaleIndex).toLanguageTag());</span><br><span class="line">      LocaleList.setDefault(locales, bestLocaleIndex);</span><br><span class="line">   <span class="comment">// 注释2_8：这里是将切换语言的动作通过 handler - message 的形式分发出去，以通知系统各个地方刷新</span></span><br><span class="line">      <span class="keyword">final</span> <span class="type">Message</span> <span class="variable">m</span> <span class="operator">=</span> PooledLambda.obtainMessage(</span><br><span class="line">               ActivityTaskManagerService::sendLocaleToMountDaemonMsg, <span class="built_in">this</span>,</span><br><span class="line">               locales.get(bestLocaleIndex));</span><br><span class="line">      mH.sendMessage(m);</span><br><span class="line">   &#125;</span><br><span class="line">   ......      </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>切换语言的流程，简单的流程就到这里，其余就不在详细展开了</p>
<h3 id="添加流程-1"><a href="#添加流程-1" class="headerlink" title="添加流程"></a>添加流程</h3><p>由前面分析可知，添加流程由 Settings 的 <strong>LocalePickerWithRegionActivity.java</strong>到 frameworks 的 <strong>LocalePickerWithRegion.java</strong><br><strong>base/core/java/com/android/internal/app/LocalePickerWithRegion.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line"></span><br><span class="line">   <span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onListItemClick</span><span class="params">(ListView l, View v, <span class="type">int</span> position, <span class="type">long</span> id)</span> &#123;</span><br><span class="line">      <span class="keyword">final</span> LocaleStore.<span class="type">LocaleInfo</span> <span class="variable">locale</span> <span class="operator">=</span></span><br><span class="line">               (LocaleStore.LocaleInfo) getListAdapter().getItem(position);</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (locale.getParent() != <span class="literal">null</span>) &#123;</span><br><span class="line">         <span class="keyword">if</span> (mListener != <span class="literal">null</span>) &#123;</span><br><span class="line">               mListener.onLocaleSelected(locale);</span><br><span class="line">         &#125;</span><br><span class="line">         returnToParentFrame();</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 注释2_9：这里是语言列表的点击事件，即代表这开启了被点击语言的添加流程</span></span><br><span class="line">      <span class="comment">// 这里是 用到本类的 createCountryPicker 方法 </span></span><br><span class="line">         <span class="type">LocalePickerWithRegion</span> <span class="variable">selector</span> <span class="operator">=</span> LocalePickerWithRegion.createCountryPicker(</span><br><span class="line">                  getContext(), mListener, locale, mTranslatedOnly <span class="comment">/* translate only */</span>);</span><br><span class="line">         <span class="keyword">if</span> (selector != <span class="literal">null</span>) &#123;</span><br><span class="line">               getFragmentManager().beginTransaction()</span><br><span class="line">                     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)</span><br><span class="line">                     .replace(getId(), selector).addToBackStack(<span class="literal">null</span>)</span><br><span class="line">                     .commit();</span><br><span class="line">         &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">               returnToParentFrame();</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">......</span><br><span class="line"></span><br><span class="line"><span class="comment">// 注释2_10_1：看上一步，调用的是这个4个参数的方法</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> LocalePickerWithRegion <span class="title function_">createCountryPicker</span><span class="params">(Context context,</span></span><br><span class="line"><span class="params">         LocaleSelectedListener listener, LocaleStore.LocaleInfo parent,</span></span><br><span class="line"><span class="params">         <span class="type">boolean</span> translatedOnly)</span> &#123;   </span><br><span class="line">      <span class="type">LocalePickerWithRegion</span> <span class="variable">localePicker</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">LocalePickerWithRegion</span>();</span><br><span class="line">      <span class="comment">// 注释2_10_2：重要是这一步，调用了localePicker.setListener，localePicker是LocalePickerWithRegion</span></span><br><span class="line">      <span class="comment">// 实例化的对象，于是将流程传递给了本类的setListener方法</span></span><br><span class="line">      <span class="type">boolean</span> <span class="variable">shouldShowTheList</span> <span class="operator">=</span> localePicker.setListener(context, listener, parent,</span><br><span class="line">               translatedOnly);</span><br><span class="line">      <span class="keyword">return</span> shouldShowTheList ? localePicker : <span class="literal">null</span>;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">static</span> LocalePickerWithRegion <span class="title function_">createLanguagePicker</span><span class="params">(Context context,</span></span><br><span class="line"><span class="params">         LocaleSelectedListener listener, <span class="type">boolean</span> translatedOnly)</span> &#123;</span><br><span class="line">      <span class="type">LocalePickerWithRegion</span> <span class="variable">localePicker</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">LocalePickerWithRegion</span>();</span><br><span class="line">      localePicker.setListener(context, listener, <span class="comment">/* parent */</span> <span class="literal">null</span>, translatedOnly);</span><br><span class="line">      <span class="keyword">return</span> localePicker;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">......</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="type">boolean</span> <span class="title function_">setListener</span><span class="params">(Context context, LocaleSelectedListener listener,</span></span><br><span class="line"><span class="params">         LocaleStore.LocaleInfo parent, <span class="type">boolean</span> translatedOnly)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.mParentLocale = parent;</span><br><span class="line">      <span class="built_in">this</span>.mListener = listener;</span><br><span class="line">      <span class="built_in">this</span>.mTranslatedOnly = translatedOnly;</span><br><span class="line">      setRetainInstance(<span class="literal">true</span>);</span><br><span class="line">   <span class="comment">// 注释2_11_1：这部分的逻辑是 获取已经添加的语言列表，作为需要忽略的部分，毕竟已经添加的，还能再添加就不合理了</span></span><br><span class="line">      <span class="keyword">final</span> HashSet&lt;String&gt; langTagsToIgnore = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;&gt;();</span><br><span class="line">      <span class="keyword">if</span> (!translatedOnly) &#123;</span><br><span class="line">         <span class="keyword">final</span> <span class="type">LocaleList</span> <span class="variable">userLocales</span> <span class="operator">=</span> LocalePicker.getLocales();</span><br><span class="line">         <span class="keyword">final</span> String[] langTags = userLocales.toLanguageTags().split(<span class="string">&quot;,&quot;</span>);</span><br><span class="line">         Collections.addAll(langTagsToIgnore, langTags);</span><br><span class="line">      &#125;</span><br><span class="line">   <span class="comment">// 注释2_11_2：这里做了选择的语言不为空的判断后，就将逻辑流程 传递给了 LocaleStore.java </span></span><br><span class="line">   <span class="comment">// 的getLevelLocales方法,从而获得一个新的已添加语言列表</span></span><br><span class="line">      <span class="keyword">if</span> (parent != <span class="literal">null</span>) &#123;</span><br><span class="line">         mLocaleList = LocaleStore.getLevelLocales(context,</span><br><span class="line">                  langTagsToIgnore, parent, translatedOnly);</span><br><span class="line">         <span class="keyword">if</span> (mLocaleList.size() &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">               <span class="keyword">if</span> (listener != <span class="literal">null</span> &amp;&amp; (mLocaleList.size() == <span class="number">1</span>)) &#123;</span><br><span class="line">                  listener.onLocaleSelected(mLocaleList.iterator().next());</span><br><span class="line">               &#125;</span><br><span class="line">               <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">         &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">         mLocaleList = LocaleStore.getLevelLocales(context, langTagsToIgnore,</span><br><span class="line">                  <span class="literal">null</span> <span class="comment">/* no parent */</span>, translatedOnly);</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">return</span> <span class="literal">true</span>;</span><br></pre></td></tr></table></figure>

<p>由于上面的添加流程，已经走完，转向了<strong>LocaleStore.java</strong> 这里，那就看看</p>
<p><strong>base/core/java/com/android/internal/app/LocaleStore.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line"></span><br><span class="line">   <span class="meta">@UnsupportedAppUsage</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">static</span> Set&lt;LocaleInfo&gt; <span class="title function_">getLevelLocales</span><span class="params">(Context context, Set&lt;String&gt; ignorables,</span></span><br><span class="line"><span class="params">         LocaleInfo parent, <span class="type">boolean</span> translatedOnly)</span> &#123;</span><br><span class="line">      <span class="comment">// 注释2_12_1：这个方法很关键，后面逻辑处理保存的数据，需要这里先做预处理的</span></span><br><span class="line">      fillCache(context);</span><br><span class="line">      <span class="type">String</span> <span class="variable">parentId</span> <span class="operator">=</span> parent == <span class="literal">null</span> ? <span class="literal">null</span> : parent.getId();</span><br><span class="line"></span><br><span class="line">      HashSet&lt;LocaleInfo&gt; result = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;&gt;();</span><br><span class="line">      <span class="keyword">for</span> (LocaleStore.LocaleInfo li : sLocaleCache.values()) &#123;</span><br><span class="line">         <span class="type">int</span> <span class="variable">level</span> <span class="operator">=</span> getLevel(ignorables, li, translatedOnly);</span><br><span class="line">         <span class="keyword">if</span> (level == <span class="number">2</span>) &#123;</span><br><span class="line">               <span class="keyword">if</span> (parent != <span class="literal">null</span>) &#123; <span class="comment">// region selection</span></span><br><span class="line">                  <span class="keyword">if</span> (parentId.equals(li.getParent().toLanguageTag())) &#123;</span><br><span class="line">                     result.add(li);</span><br><span class="line">                  &#125;</span><br><span class="line">               &#125; <span class="keyword">else</span> &#123; <span class="comment">// language selection</span></span><br><span class="line">                  <span class="keyword">if</span> (li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) &#123;</span><br><span class="line">                     result.add(li);</span><br><span class="line">                  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                     result.add(getLocaleInfo(li.getParent()));</span><br><span class="line">                  &#125;</span><br><span class="line">               &#125;</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> result;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   ......<span class="comment">// fillCache方法内部逻辑不少，只看下核心部分</span></span><br><span class="line">   </span><br><span class="line">   <span class="meta">@UnsupportedAppUsage</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fillCache</span><span class="params">(Context context)</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (sFullyInitialized) &#123;</span><br><span class="line">         <span class="keyword">return</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      ......</span><br><span class="line">      <span class="comment">// 注释2_12_2：LocalePicker.getSupportedLocales(context) 这个方法很关键 ，他是整个数据处理的来源</span></span><br><span class="line">   <span class="keyword">for</span> (String localeId : LocalePicker.getSupportedLocales(context)) &#123;</span><br><span class="line">         <span class="keyword">if</span> (localeId.isEmpty()) &#123;</span><br><span class="line">               <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllformedLocaleException</span>(<span class="string">&quot;Bad locale entry in locale_config.xml&quot;</span>);</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="type">LocaleInfo</span> <span class="variable">li</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">LocaleInfo</span>(localeId);</span><br><span class="line"></span><br><span class="line">         <span class="keyword">if</span> (LocaleList.isPseudoLocale(li.getLocale())) &#123;</span><br><span class="line">               <span class="keyword">if</span> (isInDeveloperMode) &#123;</span><br><span class="line">                  li.setTranslated(<span class="literal">true</span>);</span><br><span class="line">                  li.mIsPseudo = <span class="literal">true</span>;</span><br><span class="line">                  li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;</span><br><span class="line">               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                  <span class="comment">// Do not display pseudolocales unless in development mode.</span></span><br><span class="line">                  <span class="keyword">continue</span>;</span><br><span class="line">               &#125;</span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         <span class="keyword">if</span> (simCountries.contains(li.getLocale().getCountry())) &#123;</span><br><span class="line">               li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;</span><br><span class="line">         &#125;</span><br><span class="line">            <span class="comment">// 注释2_12_3： 这也是2_12_1处后续逻辑处理 sLocaleCache的加载的地方</span></span><br><span class="line">         sLocaleCache.put(li.getId(), li);</span><br><span class="line">         <span class="keyword">final</span> <span class="type">Locale</span> <span class="variable">parent</span> <span class="operator">=</span> li.getParent();</span><br><span class="line">         <span class="keyword">if</span> (parent != <span class="literal">null</span>) &#123;</span><br><span class="line">               <span class="type">String</span> <span class="variable">parentId</span> <span class="operator">=</span> parent.toLanguageTag();</span><br><span class="line">               <span class="keyword">if</span> (!sLocaleCache.containsKey(parentId)) &#123;</span><br><span class="line">                  sLocaleCache.put(parentId, <span class="keyword">new</span> <span class="title class_">LocaleInfo</span>(parent));</span><br><span class="line">               &#125;</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      ......</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>看到上一步，其实数据源来自LocalePicker.getSupportedLocales(context)，那就看看<br><strong>base/core/java/com/android/internal/app/LocalePicker.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line"><span class="comment">// 注释2_13_1：这个方法也很关键，这是获取系统资源配置支持的语言列表</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String[] getSystemAssetLocales() &#123;</span><br><span class="line">      <span class="keyword">return</span> Resources.getSystem().getAssets().getLocales();</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">static</span> String[] getSupportedLocales(Context context) &#123;</span><br><span class="line">   <span class="comment">// 注释2_13：好了，找到这里，似乎真相大白了，所有的语言列表，都是从这个supported_locales 资源数组获取的</span></span><br><span class="line">   <span class="comment">// 资源位置：base/core/res/res/values/locale_config.xml</span></span><br><span class="line">      String[] allLocales = context.getResources().getStringArray(R.array.supported_locales);</span><br><span class="line">      Predicate&lt;String&gt; localeFilter = getLocaleFilter();</span><br><span class="line">      <span class="keyword">if</span> (localeFilter == <span class="literal">null</span>) &#123;</span><br><span class="line">         <span class="keyword">return</span> allLocales;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      List&lt;String&gt; result = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(allLocales.length);</span><br><span class="line">      <span class="keyword">for</span> (String locale : allLocales) &#123;</span><br><span class="line">         <span class="keyword">if</span> (localeFilter.test(locale)) &#123;</span><br><span class="line">               result.add(locale);</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="type">int</span> <span class="variable">localeCount</span> <span class="operator">=</span> result.size();</span><br><span class="line">      <span class="keyword">return</span> (localeCount == allLocales.length) ? allLocales</span><br><span class="line">               : result.toArray(<span class="keyword">new</span> <span class="title class_">String</span>[localeCount]);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">......</span><br></pre></td></tr></table></figure>
<p>好了分析到这里，加载的流程也基本结束了。</p>
<h3 id="最后总结"><a href="#最后总结" class="headerlink" title="最后总结"></a>最后总结</h3><ul>
<li>R.array.supported_locales 获取到的语言列表 是config 里的xml 配置，如果要对系统做语言支持上的变动，可以改这里</li>
<li>Resources.getSystem().getAssets().getLocales()才是系统真正可以支持的语言列表，ActivityTaskManagerService.java的updateGlobalConfigurationLocked 方法里判断选择的语言是不是可用，也是用的这个资源作为判断依据</li>
<li>R.array.supported_locales里的语言配置，可能Resources.getSystem().getAssets().getLocales()不能全部支持，这就会导致我们添加了某个语言，切换到它，但是却没效果，所以有个更好的优化方案这边分享一个：</li>
</ul>
<p>修改 <strong>base/core/java/com/android/internal/app/LocalePicker.java 的 getSupportedLocales</strong> 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String[] getSupportedLocales(Context context) &#123;</span><br><span class="line">   String[] allLocales = context.getResources().getStringArray(R.array.supported_locales);</span><br><span class="line">   <span class="comment">// 设置切换语言不支持的问题__配置可支持的语言筛掉没有系统资源配置的</span></span><br><span class="line">   <span class="comment">/* </span></span><br><span class="line"><span class="comment">   Predicate&lt;String&gt; localeFilter = getLocaleFilter();</span></span><br><span class="line"><span class="comment">   if (localeFilter == null) &#123;</span></span><br><span class="line"><span class="comment">      return allLocales;</span></span><br><span class="line"><span class="comment">   &#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">   List&lt;String&gt; result = new ArrayList&lt;&gt;(allLocales.length);</span></span><br><span class="line"><span class="comment">   for (String locale : allLocales) &#123;</span></span><br><span class="line"><span class="comment">      if (localeFilter.test(locale)) &#123;</span></span><br><span class="line"><span class="comment">            result.add(locale);</span></span><br><span class="line"><span class="comment">      &#125;</span></span><br><span class="line"><span class="comment">   &#125;</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">   Predicate&lt;String&gt; localeFilter = getLocaleFilter();</span><br><span class="line">   List&lt;String&gt; result = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(allLocales.length);</span><br><span class="line">   String[] sysAssetLocales = getSystemAssetLocales();</span><br><span class="line">   <span class="keyword">for</span>(String locale : allLocales)&#123;</span><br><span class="line">      <span class="keyword">if</span>(!isCongenericLocales(sysAssetLocales,locale))&#123;</span><br><span class="line">            <span class="comment">// 本地资源配没有有配置直接跳过继续检查下一个</span></span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span>(localeFilter == <span class="literal">null</span>)&#123;</span><br><span class="line">            result.add(locale);</span><br><span class="line">      &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (localeFilter.test(locale)) &#123;</span><br><span class="line">               result.add(locale);</span><br><span class="line">            &#125;</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="type">int</span> <span class="variable">localeCount</span> <span class="operator">=</span> result.size();</span><br><span class="line">   <span class="keyword">return</span> (localeCount == allLocales.length) ? allLocales</span><br><span class="line">            : result.toArray(<span class="keyword">new</span> <span class="title class_">String</span>[localeCount]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置切换语言不支持的问题__筛查系统没有配置资源的语言</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isCongenericLocales</span><span class="params">(String[] sysAssetLocales, String xmlLocales)</span>&#123;</span><br><span class="line">   <span class="type">boolean</span> <span class="variable">result</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">   <span class="keyword">try</span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(xmlLocales !=<span class="literal">null</span>)&#123;</span><br><span class="line">            String[] xmlPartHead = xmlLocales.split(<span class="string">&quot;-&quot;</span>,<span class="number">2</span>);</span><br><span class="line">            <span class="keyword">for</span>(String assetLocales : sysAssetLocales)&#123;</span><br><span class="line">               String[] sysPartHead = assetLocales.split(<span class="string">&quot;-&quot;</span>,<span class="number">2</span>);</span><br><span class="line">               <span class="keyword">if</span>(xmlPartHead[<span class="number">0</span>].equals(sysPartHead[<span class="number">0</span>]))&#123;</span><br><span class="line">                  result = <span class="literal">true</span>;</span><br><span class="line">                  <span class="keyword">break</span>;</span><br><span class="line">               &#125;</span><br><span class="line">            &#125;</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;<span class="keyword">catch</span>(Exception e)&#123;</span><br><span class="line">      Log.e(TAG, <span class="string">&quot;Failed to deal sysAssetLocales and xmlLocales compare!&quot;</span>, e);</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2023/04/22/2023-04-22/">http://longzhiye.top/2023/04/22/2023-04-22/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2023/04/29/2023-04-29/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'WPS表格甘特图制作教程'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2023/03/17/2023-03-17/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Markdown基本语法'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
